package com.tz.actions;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.validator.annotations.*;
import com.tz.entity.User;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

@Namespace("/user")
public class UserLoginAction extends ActionSupport {
    private User user;

    @Validations(
            requiredStrings = {
                    @RequiredStringValidator(
                            type = ValidatorType.SIMPLE,
                            fieldName = "user.Email",
                            message = "邮箱不能为空!"),
                    @RequiredStringValidator(
                            type = ValidatorType.SIMPLE,
                            fieldName = "user.password",
                            message = "密码不能为空!")},
            regexFields = {
                    @RegexFieldValidator(
                            type=ValidatorType.FIELD,
                            regex = "[A-Za-z0-9]{6,20}",
                            fieldName = "user.password",
                            message = "密码必须由英文或数字组成"),
                    @RegexFieldValidator(
                    type=ValidatorType.FIELD,
                    regex = "^(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w{2,3}){1,3})$",
                    fieldName = "user.Email",
                    message = "邮箱格式不正确")}
    )
    @Action(value = "login",results = {
            @Result(location = "/index.jsp"),
            @Result(name=INPUT,location="/login.jsp")
    })
    public String userLogin(){
        user.setName("tom");
        return SUCCESS;
        //return ERROR;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
